package models;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import java.io.InputStreamReader;




///Ru



public class PlayersocketListener extends Thread {
	
	private String tabelcommandserver[]=null;
    private Integer playersnumber=0;
    private Integer startwalletplayer=0;
    private Integer mygoldingamion;
	private Integer mywallet=0;
    private Integer wallet=0;
    
    private Integer smallblind=1;
    private Integer bigblind=2;
    private Integer smallblindp=2;
    private Integer bigblindp=3;
    private Integer mynumer=1;	
    private boolean connection;
    private boolean clientalive;
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
	private Human pa ;
	private Integer firstplayer;
	private Integer maxgold;
	private Integer whatround;
	private Integer whatroundbig;
	private Integer endplayer;
    
    
    public PlayersocketListener(Human pa) {
    	connection=false;
    	clientalive=true;
    	this.pa=pa;
    	whatround=1;
    	whatroundbig=1;
    	
      }
    
    
    
    
    public Boolean showisreadyAuction()
    {
    	return pa.showisreadyAuction();
    }
    
    public Boolean showisreadyChangecard()
    {
    	return pa.showisreadyChangecard();
    }
    
    
    
    public void Order(String tabel[])
    { @SuppressWarnings("unused")
	Integer playnumer=8;
    	try { if(tabel.length==3 )
    		  {firstplayer=Integer.parseInt(tabel[1]);
    		   whatroundbig=Integer.parseInt(tabel[2]);
    		   pa.setTextL("Linforounde2",""+ whatroundbig);
    			endplayer=0;
    			for(int b=firstplayer; b<=playersnumber; b++ )
				{ if(!pa.getTextLtabel(b,1 ).equals("Allin") && !pa.getTextLtabel(b,1 ).equals("Fold" ) && !pa.getTextLtabel(b,1 ).equals("nieaktywny" ) ){
					pa.setTextLtabel("" + ++endplayer ,b,5); 
				   } else  pa.setTextLtabel("0"  ,b,5); 
					
				}
    			
    			pa.setTextL("linfo","Nowa kolejno�� " );
    			
				
				for(int h=1; h<firstplayer; h++ )
				{
					if(!pa.getTextLtabel(h,1 ).equals("Allin") && !pa.getTextLtabel(h,1 ).equals("Fold" ) && !pa.getTextLtabel(h,1 ).equals("nieaktywny" ) ){
						pa.setTextLtabel("" + ++endplayer ,h,5); 
					   } else  pa.setTextLtabel("0"  ,h,5); 
				}	
    		 } 
    	      else {}
           } 
        catch (NumberFormatException e) 
              {}
    	
    	
    	catch (ArrayIndexOutOfBoundsException e) {} 
    	
    }
    
    
    
    
    
    
    public void Infostart(String tabel[])
    	{
 
    	if(tabelcommandserver.length==13  )
    		{  try { playersnumber=Integer.parseInt(tabel[1]);
    		     pa.plset(playersnumber);
        	startwalletplayer=Integer.parseInt(tabel[2]);
        	mywallet=startwalletplayer;
        	mygoldingamion=0;
        	smallblindp=Integer.parseInt(tabel[3]);
        	bigblindp=Integer.parseInt(tabel[4]);
        	mynumer=Integer.parseInt(tabel[5]);
        	firstplayer=Integer.parseInt(tabel[10]);
        	smallblind=Integer.parseInt(tabel[11]);
        	bigblind=Integer.parseInt(tabel[12]);
        	} 
        	catch (NumberFormatException e) 
        		{}
    		
    		  pa.setColorLWhite();  
    		
    		
    		
    			for(int i=0; i<playersnumber; i++ )
    					
    				{
    				
    				
    				  int t=i+1;
    				  
    				 
      	           
         				
    				 pa.setTextLtabel("Gracz numer:" + t, t,0);
    				  pa.setmyNumeron(mynumer);
    				 
    					if(smallblindp!=t && bigblindp!=t)
    						{pa.setTextLtabel("aktywny",t,1 );
    						 pa.setTextLtabel("" +startwalletplayer + "",t,2);
    						 pa.setTextLtabel("0",t,3 );
    					}else  if(smallblindp==t)
	  				  					{ wallet=startwalletplayer -smallblind;
	  					                  pa.setTextLtabel("Ma�� ciemn� da�",t,1 );
		  				  		          pa.setTextLtabel("" + wallet + "",t,2);
		  				  		          pa.setTextLtabel("" + smallblind + "",t,3);
	  					                  if(mynumer==t)  mywallet=wallet;
	  				  					} else 
	  				  					        {wallet=startwalletplayer -bigblind;
			  					                  pa.setTextLtabel("Du�� ciemn� da�",t,1 );
				  				  		          pa.setTextLtabel("" + wallet + "",t,2);
				  				  		          pa.setTextLtabel("" + bigblind + "",t,3);
				  				  		          
				  				  		          if(mynumer==t)  mywallet=wallet;
				  				  		         
	  				  						
	  				  					        }
    					
    					pa.setTextLtabel("�adnych",t,4);
    					if(mynumer==t) 

	  		    	       {for(int k=0; k<6; k++ )
	  		    	       				{ pa.setColorL(t,k);  
	  		    	    	           
	  		    	       				}
	  		    	       
	  		    	     pa.setTextLtabel("To jeste� ty:",t,0 );
	  		    	     pa.setTextL("Linfomywallet2",tabel[2]);
	  		    	     mygoldingamion=startwalletplayer-mywallet;
	  		    	     pa.setmaxGolden(bigblind);
	  		    	     pa.setTextL("Linfomygoldingame2", Integer.toString(mygoldingamion));
	  			       	 pa.setTextL("Linfomywallet2", Integer.toString(mywallet));
	  			    	 pa.setTextL("Linfomaxgold2",Integer.toString(bigblind));
	  		    	     
	  		    	       }
    					
    				
	  		      
    				
	  		  
	  			}
    			
    			for(int i=playersnumber+1; i<=6; i++ )  
    			{pa.setTextLtabel("",i,0 );
    			 pa.setTextLtabel("",i,1 );
    			 pa.setTextLtabel("",i,2 );
    			 pa.setTextLtabel("",i,3 );
    			 pa.setTextLtabel("",i,4 );
    			 pa.setTextLtabel("",i,5 );
    			
    			}
    			
    			endplayer=0;
    			for(int b=firstplayer; b<=playersnumber; b++ )
				{
					pa.setTextLtabel("" + ++endplayer ,b,5); 
					
				}
				
				for(int h=1; h<firstplayer; h++ )
				{
					pa.setTextLtabel("" + ++endplayer ,h,5);
				}
		   
	 
	 
    			pa.setTextB("Bcard1",tabel[9]);
    			pa.setTextB("Bcard2",tabel[6]);
    			pa.setTextB("Bcard3",tabel[7]);
    			pa.setTextB("Bcard4",tabel[8]);
    		}
    	
    	}
    
    
    
    
    public void Infostart2(String tabel[])
	{

	if(tabelcommandserver.length==13  )
		{  try { playersnumber=Integer.parseInt(tabel[1]);
		     pa.plset(playersnumber);
    	startwalletplayer=Integer.parseInt(tabel[2]);
    	mywallet=startwalletplayer;
    	mygoldingamion=0;
    	smallblindp=Integer.parseInt(tabel[3]);
    	bigblindp=Integer.parseInt(tabel[4]);
    	mynumer=Integer.parseInt(tabel[5]);
    	firstplayer=Integer.parseInt(tabel[10]);
    	smallblind=Integer.parseInt(tabel[11]);
    	bigblind=Integer.parseInt(tabel[12]);
    		} 
    	catch (NumberFormatException e) 
    		{}
		
		  pa.setColorLWhite();  
		  whatround=1;
	    whatroundbig=1;	
		
		
			for(int i=0; i<playersnumber; i++ )
					
				{
				
				
				  int t=i+1;
				  
				 
  	           
     				
				 pa.setTextLtabel("Gracz numer:" + t, t,0);
				  pa.setmyNumeron(mynumer);
				 
					if(smallblindp!=t && bigblindp!=t)
						{pa.setTextLtabel("aktywny",t,1 );
						 try {	 wallet= Integer.parseInt(pa.getTextLtabel(t,2));} 
			            	catch (NumberFormatException e) 
	            				{}
						 pa.setTextLtabel("" +wallet + "",t,2);
						 if(mynumer==t)  mywallet=wallet;
						 pa.setTextLtabel("0",t,3 );
					}else  if(smallblindp==t)
  				  					{ 
  					                  pa.setTextLtabel("Ma�� ciemn� da�",t,1 );
	  				  		          try {	 wallet= Integer.parseInt(pa.getTextLtabel(t,2))-smallblind;} 
  					            	catch (NumberFormatException e) 
  					            				{}
  					                  pa.setTextLtabel("" + wallet + "",t,2);
	  				  		          pa.setTextLtabel("" + smallblind + "",t,3);
  					                  if(mynumer==t)  mywallet=wallet;
  				  					} else 
  				  					        {
		  					                  pa.setTextLtabel("Du�� ciemn� da�",t,1 );
			  				  		          try {	 wallet= Integer.parseInt(pa.getTextLtabel(t,2)) -bigblind;} 
  					            	catch (NumberFormatException e) 
  					            				{}
		  					                  pa.setTextLtabel("" + wallet + "",t,2);
			  				  		          pa.setTextLtabel("" + bigblind + "",t,3);
			  				  		          
			  				  		          if(mynumer==t)  mywallet=wallet;
			  				  		         
  				  						
  				  					        }
					
					pa.setTextLtabel("�adnych",t,4);
					if(mynumer==t) 

  		    	       {for(int k=0; k<6; k++ )
  		    	       				{ pa.setColorL(t,k);  
  		    	    	           
  		    	       				}
  		    	       
  		    	     pa.setTextLtabel("To jeste� ty:",t,0 );
  		    	     pa.setTextL("Linfomywallet2",tabel[2]);
  		    	     mygoldingamion=startwalletplayer-mywallet;
  		    	     pa.setmaxGolden(bigblind);
  		    	     pa.setTextL("Linfomygoldingame2", Integer.toString(mygoldingamion));
  			       	 pa.setTextL("Linfomywallet2", Integer.toString(mywallet));
  			    	 pa.setTextL("Linfomaxgold2",Integer.toString(bigblind));
  		    	     
  		    	       }
					
				
  		      
				
  		  
  			}
			
			for(int i=playersnumber+1; i<=6; i++ )  
			{pa.setTextLtabel("",i,0 );
			 pa.setTextLtabel("",i,1 );
			 pa.setTextLtabel("",i,2 );
			 pa.setTextLtabel("",i,3 );
			 pa.setTextLtabel("",i,4 );
			 pa.setTextLtabel("",i,5 );
			
			}
			
			endplayer=0;
			for(int b=firstplayer; b<=playersnumber; b++ )
			{
				pa.setTextLtabel("" + ++endplayer ,b,5); 
				
			}
			
			for(int h=1; h<firstplayer; h++ )
			{
				pa.setTextLtabel("" + ++endplayer ,h,5);
			}
	   
 
 
			pa.setTextB("Bcard1",tabel[9]);
			pa.setTextB("Bcard2",tabel[6]);
			pa.setTextB("Bcard3",tabel[7]);
			pa.setTextB("Bcard4",tabel[8]);
		}
	
	}
    
    
    
    public void Newcard(String tabel[])
    { 
	Integer playnumer=8;
    	try {  if(tabel.length==6 )
    			{playnumer=Integer.parseInt(tabel[1]);
    				if(mynumer==playnumer  )
    				{pa.setTextB("Bcard1",tabel[2]);
        			pa.setTextB("Bcard2",tabel[3]);
        			pa.setTextB("Bcard3",tabel[4]);
        			pa.setTextB("Bcard4",tabel[5]);
    				}
    			} 
    	      else {}
           } 
        catch (NumberFormatException e) 
              {}
    	
    	
    	catch (ArrayIndexOutOfBoundsException e) {} 
    	
    }
    
    public void Winner(String tabel[])
    { String infow,infow2;
    int sum;
	int playnumer;
    	try { 
    		if(tabel[1].equals("info")  )
    			{infow=" ";
    		     infow2=" ";
    		     
    		   
    		    
    			if(tabel.length==3)
    				{  
    				 playnumer=Integer.parseInt(tabel[2]);
    				
    				   sum=Integer.parseInt(pa.getTextLtabel(playnumer,2));
    				   
    				   if(mynumer==playnumer)
    				   { infow="Wygra�e�";
    				    
    				   
    				   
    				   if(!pa.getTextLtabel(playnumer,1).equals("Allin") )
    				     { sum=sum+maxgold*playersnumber;  pa.setTextLtabel("" +sum,playnumer,2);}
    						   else{int gwin= sum=Integer.parseInt(pa.getTextLtabel(playnumer,3));
    						        
    						        int pwin= sum=Integer.parseInt(pa.getTextLtabel(playnumer,2));
    							   for(int b=1; b<=playersnumber; b++ )
    						  
    								{if(playnumer!=b)
    							     {  
    									int gingame= sum=Integer.parseInt(pa.getTextLtabel(b,3));
    									int gg= sum=Integer.parseInt(pa.getTextLtabel(b,2));
    									if(gingame>=gwin)
    							    
    									{pwin=gwin+pwin;
    									
    									pa.setTextLtabel("" + pwin,playnumer,2);  
    									
    									gg= gg+gingame-gwin;
    									pa.setTextLtabel("" + gg,b,2);  
    									
    									}else {pwin=pwin+gingame;
    									      pa.setTextLtabel("" + pwin,playnumer,2);  
    									      }
    							     
    							     }
    							   
    								}
    						  
    				    //  pa.setTextLtabel("" +sum,playnumer,2);
    				      
    						   }
    				   }
    				   else { infow="Wygra� gracz: " + tabel[2];
    				  
    				   if(!pa.getTextLtabel(playnumer,1).equals("Allin") )
    				   { sum=sum+maxgold*playersnumber;  pa.setTextLtabel("" +sum,playnumer,2);}
  						   else{int gwin= sum=Integer.parseInt(pa.getTextLtabel(playnumer,3));
  						        
  						        int pwin= sum=Integer.parseInt(pa.getTextLtabel(playnumer,2));
  							   for(int b=1; b<=playersnumber; b++ )
  						  
  								{if(playnumer!=b)
  							     {  
  									int gingame=Integer.parseInt(pa.getTextLtabel(b,3));
  									int gg=Integer.parseInt(pa.getTextLtabel(b,2));
  									if(gingame>=gwin)
  							    
  									{pwin=gwin+pwin;
  									
  									pa.setTextLtabel("" + pwin,playnumer,2);  
  									
  									gg= gg+gingame-gwin;
  									pa.setTextLtabel("" + gg,b,2);  
  									
  									}else {pwin=pwin+gingame;
  									      pa.setTextLtabel("" + pwin,playnumer,2);  
  									      }
  							     
  							     }
  							   
  								}
  						  
  				    //  pa.setTextLtabel("" +sum,playnumer,2);
  				      
  						   }
    				    
    				   		}
    				
    				
    				}
    			else{  for(int j=2;j<tabel.length; j++)
		     			{  playnumer=Integer.parseInt(tabel[j]);
		     			   
		     				if(mynumer==playnumer)
		     				{ infow="Wygra�e� ty oraz gracze:";   }
		    				   else { if(infow2.equals(" ")) {infow2= infow2 + "" + tabel[j];  }
		    				          else {infow2= infow2 + " i " + tabel[j]; 
		    				        }
		     			}
				
				    if(infow.equals("Wygra�e� ty oraz gracze:"))
				    	{
				    	infow=infow+infow2;
				    	}
				    else { infow="Wygrali gracze:"+infow2;
				    	
				    	}
					}
    			
    				
    				
    			}
    			pa.setTextL("Linfo", infow);	
    		}
    	}
        catch (NumberFormatException e) 
              {}
    	
    	
    	catch (ArrayIndexOutOfBoundsException e) {} 
    	
    }
    
    
    
    public void Info(String tabel[])
    { @SuppressWarnings("unused")
	Integer playnumer=8;
    	try {  if(tabel.length==3 )
    			{playnumer=Integer.parseInt(tabel[1]);
    			playnumer=Integer.parseInt(tabel[1]);
    			playnumer=Integer.parseInt(tabel[1]);
    			playnumer=Integer.parseInt(tabel[1]);
    				if(tabel[1].equals("info")  )
    				{playnumer=Integer.parseInt(tabel[1]);
    					
    					pa.setTextL("Info", tabel[2]);}
    			} 
    	      else {}
           } 
        catch (NumberFormatException e) 
              {}
    	
    	
    	catch (ArrayIndexOutOfBoundsException e) {} 
    	
    }
    
    /// Format data from server; " carchange youraction playnumer whatrunde" || " carchange info playnumer howmanycardchange" variables=playnumer, howmanycardchange; regular tekst=Auction, youraction, info 
    public void Changecard(String tabel[])
    { Integer playnumer=8;
     
    	try {  if(tabel.length==3 ||tabel.length==4)
    				{playnumer=Integer.parseInt(tabelcommandserver[2]);
    				if(tabel[1].equals("yourchangecard") &&  mynumer==playnumer )
            			{pa.setyoucanChangecard("yes");
            			 pa.setTextL("Linfo", "Kliknij na karty do wymiany");
            			 pa.setTextB("Bok", "Zatwierdz wymiane kart");
            			}
    				else if(tabel[1].equals("info") && tabel.length==4) { pa.setTextLtabel(tabel[3],playnumer,4);}
    				     else  {}
    			}
    			else {}
    	        
        } 
        catch (NumberFormatException e) 
              {}
    	
    	catch (ArrayIndexOutOfBoundsException e) {} 
    	 
    	
    }
    
   
  
    
    /// Format data from server; " Auction youraction playnumer welcherunde" || " Auction info playnumer  condition numberoftokens rate " variables=playnumer, howmanycardchange; 
    public void Auction(String tabel[])
    { 
      Integer playnumer=8;
      Integer whatgolddifferent;
      Integer whatgolddifferent2;
    	try { 
    		
    	
    		if(tabel.length==4 || tabel.length==6)
    			{playnumer=Integer.parseInt(tabel[2]);
    			 
    			
    			 
    			if(tabel[1].equals("yourauction") &&  mynumer==playnumer )
    				{ pa.setTextL("Linfo", "Twoja tura, wybierz co robisz");
    				    if(whatroundbig!=1)
    				    {	if(Integer.parseInt(tabel[3])!=whatround) 
    							{whatround=Integer.parseInt(tabel[3]); pa.setlongFalse(); }
    							else { pa.setlongTrue(); 
    								}
    				    }
    				    else {pa.setlongTrue(); 
									  }
    				  
    				
    				
    				 pa.setyoucanAuction("yes");
    				 pa.setTextL("Linfo", "Twoja tura, wybierz co robisz2");
    				 pa.setTextB("Bok", "Zatwierdz akcje licytacji");
    				// pa.setTextL("Linforunde2","" + whatround);
    				
    				
    				}
    			else if(tabel[1].equals("info") && tabel.length==6) 
    				{  whatgolddifferent=Integer.parseInt(tabel[5]);
       			       whatgolddifferent2=Integer.parseInt(pa.getTextLtabel(playnumer,3));
    				
    				
    				if(whatgolddifferent2<whatgolddifferent) { maxgold=whatgolddifferent; pa.setmaxGolden(whatgolddifferent); pa.setTextL("Linfomaxgold2", tabel[5]);}
    				    pa.setTextLtabel(tabel[3],playnumer,1);
    					pa.setTextLtabel(tabel[4],playnumer,2 );
    					pa.setTextLtabel(tabel[5],playnumer,3);
    					pa.setoldaction(tabel[3]);
	  		          
       	    	     
       	    	    } else {}
    			} else {}
	      
        } 
        catch (NumberFormatException e) 
              {}
    	
    	catch (ArrayIndexOutOfBoundsException e) {} 
    	 
    	
    }
    
    
	
    public String sendChangecard()
	{ String commandcard="deleteandgivenewcards ";
	 String variable="";
	 Integer nochangecard=0;
	 Integer changecard=0;
    	if(pa.showcarddelete("iscard1"))
				{
    		variable= variable + " " +pa.getTextB("Bcard1");
				} else nochangecard++;
    	
    	
    	if(pa.showcarddelete("iscard2"))
		{
        variable= variable + " " +pa.getTextB("Bcard2") ;
		}else nochangecard++;
    	
    	if(pa.showcarddelete("iscard3"))
		{
        variable= variable + " "  + pa.getTextB("Bcard3");
		}else nochangecard++;
    	
    	if(pa.showcarddelete("iscard4"))
		{
    		variable= variable + " " +pa.getTextB("Bcard4");
		}else nochangecard++;
    	
    	if(nochangecard==4)  commandcard= commandcard +"empty";
    	
    	else  {  changecard=4-nochangecard; commandcard= commandcard +  changecard  +variable; }
    	
    	return commandcard;
    	

	}
    
    public String sendAuction()
   	{ String commandauction="auctionmydecision ";
      String vari;
   	   commandauction=  commandauction +pa.showauction();
       	if(!pa.showhowgold().equals("empty") && (pa.showauction().equals("Raise") || pa.showauction().equals("Bet"))) 
       	{
       	    vari= " " +pa.showhowgold();
       		commandauction=commandauction +vari;
       	}
       	pa.setTextL("Linfo", commandauction);
       	
       	return commandauction;
      

   	}
	
            @Override
        public void run()  	
        
        	{ 	 
            	while (clientalive)
        		{ try
        		   { 
        			
        		   
        			if((pa.getTextL("Linfo").equals("Connect") ||   pa.getTextL("Linfo").equals("Connect??") ) && connection==false )
        		   				{		  try {  
        		   					         
        		   							String[] pob;
        		   				            pob=null; pob= pa.getTextF().split(" ");
        		   				             if(pob.length==2) {socket = new Socket(pob[0],  Integer.parseInt(pob[1]));}
        		                            else  {socket = new Socket("localhost", 4777); socket.setSoTimeout(16000); }
        		   	          		  				out = new PrintWriter(socket.getOutputStream(), true);
        		   	          		  				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        		   	          		  				connection=true;
        		   	          		  			pa.setTextB("Bok","Czekaj, je�li chcesz");
        		   	          					    } 
        		   							catch (IOException ex) 
        		   							{     pa.setTextB("Bok", "Problem z po��czeniem z serwerem. Spr�buj jeszcze raz");
        		   							     
        		   							} 
        		   					
        		   							catch (NumberFormatException ex)
        		   							{      pa.setTextB( "Bok" ,"B��dny numer portu");
        		   						           
        		   							}
        		   							
        			
        		   					}
        		    
        		     if(connection==true )
        		    
        		     {  String  input ;
        		      
        		    	 if((input=in.readLine())!=null)
				       { 
				         
        		    	
        		    	
        		    	 tabelcommandserver=null;
        		    	 tabelcommandserver= input.split(" ");
        		    	 socket.setSoTimeout(61000000); 
        		    	 if (tabelcommandserver[0].equals("infostart"))  
					  				{ pa.setTextL("Linfo","Zaczynamy gre");
					  				pa.setTextB("Bok","Czekaj na dane");
					  				 Infostart(tabelcommandserver);
					  			  
						  
					  				}
        		    	 else if (tabelcommandserver[0].equals("infostart2"))  
			  				{ pa.setTextL("Linfo","Zaczynamy gre");
			  				pa.setTextB("Bok","Czekaj na dane");
			  				 Infostart2(tabelcommandserver);
			  			  
				  
			  				}
        		    	 else  	 	 if (tabelcommandserver[0].equals("auction"))  
					  								{
        		    		 						Auction(tabelcommandserver);
        		    		 							if(pa.showyoucanAuction()) 
						                            			{
        		    		 								
						                            		     
        		    		 							
						                            			
						                            			
						                            			while (pa.showisreadyAuction()==false)
						                            			{
						                            				Thread.sleep(10);
						                            			}
						                            				if (pa.showisreadyAuction()==true);
			  														{
			  														out.println(sendAuction());
			  										          		pa.setEmptyauction();
			  										          		pa.setEmptyhowgold();
			  										          		pa.setisreadyAuction("false");
			  														}
						                            			}
					  								}
        		    	 else   if (tabelcommandserver[0].equals("changehand"))  
					  			   							{  
						  									Changecard(tabelcommandserver);
						  									if(pa.showyoucanChangecard()) 
						  										{ 
						  										   while (pa.showisreadyChangecard()==false)
						  											{
						                            				Thread.sleep(10);
						  											}
						                            				
						  										if(pa.showisreadyChangecard()==true)
						  												{
						  												out.println(sendChangecard());
						  												pa.setEmptycardchange();
						  												 pa.setisreadyChangecard("false");
						  												}
						  									
						  									
						  										}
					  			   							}
					  else   if (tabelcommandserver[0].equals("order"))  
 						{ Order(tabelcommandserver); }
					  else   if (tabelcommandserver[0].equals("newcard"))  
 						{  Newcard(tabelcommandserver); }
					  
					  else   if (tabelcommandserver[0].equals("infochange"))  
   						{ }
					  else   if (tabelcommandserver[0].equals("win"))  
						{Winner(tabelcommandserver); } 
        		    	 
					  else   if (tabelcommandserver[0].equals("end"))  
 						{pa.setTextB("Bok", "Problem z po��czeniem z serwerem. Spr�buj jeszcze raz"); connection=false; }
        		    	 
					  else   if (tabelcommandserver[0].equals("wait"))  
						{ }
					  
						  
					  //	if(in.readLine()==null) { pa.setTextB("Bok", "Problem z po��czeniem z serwerem. Spr�buj jeszcze raz");}
					  
        		      } else connection=false;
        		      
        		     } 
					} catch (IOException e) 
        		   {
						 pa.setTextB("Bok", "Problem z po��czeniem z serwerem. Spr�buj jeszcze raz");
					 connection=false;
					 
					 
				   } catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}  
        		
        		 
					
        			try {
							sleep(2000);
							} catch (InterruptedException e) {
							// TODO Auto-generated catch block
						
						}  
        	     } 
              	
            	try 	{   out.close();  
				in.close();
				socket.close();
			} catch (IOException e1) { pa.setTextB("Linfo", "Problem z  serwerem. Spr�buj jeszcze raz");
		                 
						}  
          catch (NullPointerException e1) {
		
	              } 
              
            	
            	
            	
            	
          
            	
     
     	 			 
        	  }
        	
        	
    }


